package ui.app.com.ndktest;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import ui.app.com.utile.AdbPower;
import ui.app.com.utile.SerialPortFinder;
import ui.app.com.utile.SerialPortUtil;

public class MainActivity extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        //
        findViewById(R.id.ck_open_tv).setOnClickListener(this);
        findViewById(R.id.ck_write_tv).setOnClickListener(this);
        findViewById(R.id.cks_tv).setOnClickListener(this);
        findViewById(R.id.cks_path_tv).setOnClickListener(this);


    }

    private SerialPortFinder serialPortFinder;

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.cks_tv:
                String apkRoot = "chmod 777 " + getPackageCodePath();
                AdbPower.runRootCommand(apkRoot);
                if (serialPortFinder == null) {
                    serialPortFinder = new SerialPortFinder();
                }
                String[] devs = serialPortFinder.getAllDevices();
                if (devs == null || devs.length == 0) {
                    Log.e("----", "没有可以用的串口");
                    break;
                }
                for (int i = 0; i < devs.length; i++) {
                    Log.e("----", "串口:" + devs[i]);
                }
                break;
            case R.id.cks_path_tv:
                if (serialPortFinder == null) {
                    serialPortFinder = new SerialPortFinder();
                }
                String[] paths = serialPortFinder.getAllDevicesPath();
                if (paths == null || paths.length == 0) {
                    Log.e("----", "没有可以用的串口");
                    break;
                }
                for (int i = 0; i < paths.length; i++) {
                    Log.e("----", "串口路径:" + paths[i]);
                }
                break;
            case R.id.ck_open_tv:
                //SerialPortUtil.getInstance().openSrialPort("tty", 9600);
               SerialPortUtil.getInstance().openSrialPort("ttyS2", 9600);

                break;
            case R.id.ck_write_tv:
                SerialPortUtil.getInstance().sendSerialPort("------测试------");
                break;
        }
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }
}
